package com.lefu.es.blenew.utils;

import android.text.TextUtils;

import com.lefu.es.db2.MacDao;
import com.lefu.es.entity.DeviceMacDao;
import com.lefu.es.util.LogUtil;
import com.lefu.es.util.LogUtils;

import java.io.ByteArrayOutputStream;
import java.util.List;

/**
 * 作者: andy on 2016/10/26.
 * 作用:字符串工具类
 */
public class StringUtils1 {
    private final static int CF_SCALE_TYPE = 1;
    private final static int CE_SCALE_TYPE = 2;
    private final static int CA_SCALE_TYPE = 3;
    private final static int CB_SCALE_TYPE = 4;
    private static String TAG = "StringUtils1";

    /**
     * 字节数组转字符串
     */
    public static String bytes2HexString(byte[] b) {
        String ret = "";
        for (int i = 0; i < b.length; i++) {

            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex;
        }

        return ret;
    }

    /**
     * 16进制转10进制
     */
    public static int hexToTen(String hex) {
        if (null == hex || (null != hex && "".equals(hex))) {
            return 0;
        }
        return Integer.valueOf(hex, 16);
    }

    /**
     * 16进制字符串转字节数组
     */
    public static byte[] hexStringToByteArray(String digits) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            for (int i = 0; i < digits.length(); i += 2) {
                char c1 = digits.charAt(i);
                if ((i + 1) >= digits.length()) {
                    throw new IllegalArgumentException("hexUtil.odd");
                }
                char c2 = digits.charAt(i + 1);
                byte b = 0;
                if ((c1 >= '0') && (c1 <= '9'))
                    b += ((c1 - '0') * 16);
                else if ((c1 >= 'a') && (c1 <= 'f'))
                    b += ((c1 - 'a' + 10) * 16);
                else if ((c1 >= 'A') && (c1 <= 'F'))
                    b += ((c1 - 'A' + 10) * 16);
                else
                    throw new IllegalArgumentException("hexUtil.bad");

                if ((c2 >= '0') && (c2 <= '9'))
                    b += (c2 - '0');
                else if ((c2 >= 'a') && (c2 <= 'f'))
                    b += (c2 - 'a' + 10);
                else if ((c2 >= 'A') && (c2 <= 'F'))
                    b += (c2 - 'A' + 10);
                else
                    throw new IllegalArgumentException("hexUtil.bad");
                baos.write(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return (baos.toByteArray());
    }

    /**
     * 获取到秤广播中的测量数据
     *
     * @param data
     * @return
     */
    public static String getReceiveScaleData(String data) {
        if (!TextUtils.isEmpty(data)) {
            String temp = data.substring(38, 60);
            LogUtil.e("****处理后的测试数据-->" + temp);
            return temp;
        }
        return "";
    }

    /**
     * 获取到秤广播中的测量数据1
     *
     * @param data
     * @return
     */
    public static String getReceverScaleData1(String data) {
        if (!TextUtils.isEmpty(data)) {
            String temp = data.substring(40, 62);
            LogUtils.d(TAG, "处理后的测试数据：" + temp);
            return temp;
        }
        return "";
    }

    /**
     * 对接收的数据进行秤类型判断
     */
    public static int getScaleTypeByData(String dataStr) {
        if (dataStr != null) {
            if (dataStr.startsWith("cf")) {
                return CF_SCALE_TYPE;
            } else if (dataStr.startsWith("ce")) {
                return CE_SCALE_TYPE;
            } else if (dataStr.startsWith("ca")) {
                return CA_SCALE_TYPE;
            } else if (dataStr.startsWith("cb")) {
                return CB_SCALE_TYPE;
            } else {
                return 0;
            }
        }
        return 0;
    }

    /**
     * 判断是否为其锁定数据
     */
    public static boolean isLockData(String data) {
        if (!TextUtils.isEmpty(data)) {
            String data1 = data.substring(18, 20);
            LogUtil.e("****isLockData-->" + data1);
            if ("00".equals(data1)) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 是不是脂肪秤，人体秤，营养秤，婴儿秤的广播数据
     * 对接收的数据进行秤类型判断是不是有效的测试数据
     */
    public static boolean isScaleData(String dataStr) {
        if (dataStr != null) {
            if (dataStr.startsWith("cf") || dataStr.startsWith("cb")) {
                return true;
            }
        }
        return false;
    }

    public static List<DeviceMacDao> getBindDeviceMac() {
        /***查询所有据***/
        List<DeviceMacDao> deviceMacs = MacDao.queryAll();
       LogUtil.e("****等到绑定MAC-->" + deviceMacs.size());
        return deviceMacs;
    }

    public static boolean isBindDevice(String mac) {
        if (!TextUtils.isEmpty(mac)){
            List<DeviceMacDao> deviceMacDaos = MacDao.queryAll();
            if (deviceMacDaos!=null && !deviceMacDaos.isEmpty()){
                for (int i = 0; i < deviceMacDaos.size(); i++) {
                    if (mac.equals(deviceMacDaos.get(i).getMac())){
                        return true;
                    }
                }
            }else{
                return false;
            }
            return false;
        }
        return false;
    }

}
